package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.common.dto.ResDevice;
import com.bjb.vr.common.service.ResDeviceService;
import com.bjb.vr.common.utils.RedisUtil;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.redis.constants.RedisConstants;
import com.bjb.vr.remote.ws.websocket.entity.Action;
import com.bjb.vr.remote.ws.websocket.entity.Command;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketMsgHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/3/29
 */
@Slf4j
@Component
public class MultiCloseVRMessageHandler implements MessageHandler {

    @Autowired
    ResDeviceService resDeviceService;

    @Autowired
    RedisUtil redisUtil;

    final String  LOCK_KEY = "res_device_multi_close_state";

    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {
        String dbLockKey = RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY + LOCK_KEY+ rabbitWebSocketMsg.getGroupCode();



        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);

        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_MULTI_CLOSE_VR)){
            log.info("处理多台设备关机信息");

            JSONObject _jsonObject = JSONObject.parseObject(message.getCommand().getParameter());
            JSONArray jsonArray = _jsonObject.getJSONArray("devUdids");

            if(jsonArray!=null  && jsonArray.size()>0){
                Action action = new Action();
                action.setAction(MessageActionConstants.MESSAGE_ACTION_CLOSE_VR);
                Command command = new Command<>();
                for(int i=0 ; i<jsonArray.size() ; i++){
                    String udid = (String) jsonArray.get(i);
                    //command.setUdid(udid);
                    action.setCommand(command);
                    rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
                    message.getCommand().setUdid(udid);
                    WebSocketMsgHandler.sendSingleClient(rabbitWebSocketMsg,message);
                }
                //设置为空消息，防止其他handler处理
                action.setAction(MessageActionConstants.MESSAGE_ACTION_EMPTY);
                rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
            }

            String value = String.valueOf(System.currentTimeMillis());
            if(redisUtil.lock(dbLockKey,value, RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY_EXPIRE_TIME)) {
                List<ResDevice> resDeviceList = new ArrayList<>();
                Date updateDate = new Date();
//                for(int i=0 ; i<jsonArray.size() ; i++){
//                    ResDevice resDevice = new ResDevice();
//                    resDevice.setMode();
//                }


//                resDeviceService.updateResDeviceLockStateByUdid(message.getCommand().getUdid(), _jsonObject.getInteger("lockState"));
                redisUtil.unlock(dbLockKey, value);
            }

        }

    }
}
